<?php
namespace Act\Os;
use \Lib\ErrorMessage;
use \Lib\Common;
class Quicknote extends Backend{
	public function doAdd(){
		if(is_post()){
			$note_content = post('note_content','','htmlspecialchars');
			if($note_content ==""){
				$this->alert("error",ErrorMessage::NEED_PARAM);
			}else{
				$input_data = array ('note_content' => $note_content, 'owner_id' => session('uid'));
				$note_id = model('#QuickNote','adm_')->addNote ( $input_data );
				
				if ($note_id) {
					$this->sysLog( session('user_info')['user_name'], 'ADD', 'QuickNote' ,$note_id, json_encode($input_data) );
					return $this->success ('便签添加成功','os/quicknote/add');
				}
			}
		}
		$this->display('from.php');
	}

	public function doUpdate(){
		$note_id = get('note_id',0,'absint');
		$this->checkParam($note_id);
		$quicknote_mod = model('#QuickNote','adm_');
		$quicknote = $quicknote_mod->getNoteById ( $note_id );
		if(empty($quicknote)){
			return $this->error(ErrorMessage::QUICKNOTE_NOT_EXIST,"os/quicknotes");
		}

		if (is_post()) {
			$note_content = Common::filterText(post('note_content'));
			if($note_content =="" ){
				$this->alert("error",ErrorMessage::NEED_PARAM);
			}else{
				$current_user_info=session('user_info');
				$user_group = $current_user_info['user_group'];
				$current_user_id = $current_user_info['user_id'];
				if($user_group ==1 || $quicknote['owner_id'] == $current_user_id){
					// $note_content = htmlspecialchars($note_content);
					$update_data = array ('note_content' => $note_content);
					$result = $quicknote_mod->updateNote( $note_id,$update_data );
					
					if ($result>=0) {
						$this->sysLog($current_user_info['user_name'], 'MODIFY', 'QuickNote' ,$note_id, json_encode($update_data) );
						return $this->success ('更新完成','os/quicknotes');
					} else { 
						$this->alert("error");
					}
				}else{
					$this->alert("error",ErrorMessage::QUICKNOTE_NOT_OWNER);
				}
			}
		}
		$_POST = $quicknote;
		$this->display('from.php');
	}
}